// pages/accounts/search/search.js
const app = getApp();
const globalData = getApp().globalData;
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const leadService = require("../../../apiservice/lead/leadservice");


Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		searchKey: "",
		height: 0,
		isnextPage: false,
		pagenum: 0,
		total: 0,
		leadid: ""
	},

	async GetPage(conditions, pageNum, pageSize) {

		try {
			let userInfo = globalData.userInfo;
			var result = await leadService.QueryLeadPageByConditionsAndOwner(conditions, pageNum, 20, userInfo.userid)

			if (result.data && result.data && result.data.datas) {
				var displaytotal = this.data.displaytotal + result.data.datas.length;
				this.setData({
					['list[' + (pageNum - 1) + ']']: result.data.datas
				});
				this.setData({
					pagenum: pageNum,
					displaytotal: displaytotal,
					isnextPage: result.data.existsnextdata,
					total: result.data.total
				})
				console.log(result.data);
			} else {
				this.setData({
					pagenum: 1,
					displaytotal: 0,
					isnextPage: false,
					total: 0
				})
			}

		} catch (e) {
			//TODO handle the exception

		} finally {
			wx.hideLoading();
		}
	},

	lower: async function () {
		//debugger;
		if (this.data.isnextPage) {
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
				title: '加载中',
				icon: 'loading'
			});
			var pageNum = this.data.pagenum;
			var searhKey = this.data.searchKey
			await this.GetPage(searhKey, ++pageNum, 20);
		} else {
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '我也是有底线的',
				icon: 'success',
				duration: 350
			});
			return false;
		}
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var res = wx.getSystemInfoSync();
		this.setData({
			height: res.windowHeight
		});
	},

	onSearch: async function (e) {
		if (!e.detail.searchKey) {
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '请输入搜索条件',
				icon: 'warn',
				duration: 550
			});
			return false;
		} else {
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
				title: '加载中',
				icon: 'loading'
			});

			this.setData({
				list: null,
				searchKey: e.detail.searchKey
			})

			await this.GetPage(e.detail.searchKey, 1, 20)
		}


	},

	enterDetail(e) {
		console.log(e.currentTarget.id);
		wx.navigateTo({
			url: '../detail/detail?leadId=' + e.currentTarget.id
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})
